//引入第三方express模块
const express=require("express");
//引入第三方路由器模块
const router=express.Router();
//引入连接池
const pool=require("../pool")

//创建路由，端口
router.get("/",(req,res)=>{
  var sql="select * from user_list"
  pool.query(sql,[],(err,result)=>{
    if(err) console.log(err);
    res.send(result)
  })
})

router.get("/check",(req,res)=>{
  var uid=req.session.uid;
  console.log(req.session.uid)
  var sql="select uname,uid from user_list where uid=?"
  pool.query(sql,[uid],(err,result)=>{
    if(err) throw err
    res.send(result)
  })
})

router.get("/out",(req,res)=>{
  req.session.uid="";
  res.send({code:1})
})

// 注册
router.post("/reg",(req,res)=>{
  //console.log("进端口")
  var result=req.body;
  //console.log("值传进来了")
  var uname=result.uname;
  var upwd=result.upwd;
  var uphone=result.uphone;
  //console.log(uname,upwd,uphone);
  var sql="INSERT INTO user_list VALUES(null,?,?,null,?,null,null)"
  pool.query(sql,[uname,upwd,uphone],(err,result)=>{
    if(err) console.log(err);
   // console.log(result);
    if(result.affectedRows>0){
      res.send("200")
    }
  })

  var sqlTwo="INSERT INTO useraccount VALUES(null,?,0)";
  pool.query(sqlTwo,[uname],(err,result)=>{
    if(err) throw err
    console.log(result)
  })
})

// 登录
router.post('/login',(req,res)=>{
  var uname=req.body.uname;
  var upwd=req.body.upwd
  var sql="select uid from user_list where uname=? AND upwd=?"
  pool.query(sql,[uname,upwd],(err,result)=>{
    if(err) throw err;
    if(result.length>0){
      var uid=result[0].uid
      req.session.uid=uid
      res.send({code:200,msg:"登录成功！！！"})
    }else{
      res.send({code:-1,msg:"用户名或者密码错误！！！"})
    }
  })
})




//修改信息
router.post('/update',(req,res)=>{
  var result=req.body;
  var uid=result.uid;
  var uname=result.uname;
  var upwd=result.upwd;
  var uphone=result.uphone;
  var uemail=result.uemail;
  console.log(uid,uname,upwd,uphone,uemail);
})
//公开模块
module.exports=router;


